package com.wing.qualification.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.config.BaseDataInit;
import com.wing.platform.feign.PlatformClient;
import com.wing.qualification.dao.QualificationDao;
import com.wing.qualification.model.entity.Qualification;
import com.wing.qualification.model.request.QualificationForm;
import com.wing.qualification.model.response.QualificationVO;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 资质管理-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class QualificationServiceImpl extends ServiceImpl<QualificationDao, Qualification> implements QualificationService {
    @Autowired
    private PlatformClient platformClient;
    @Override
    @Transactional
    public int add(QualificationForm qualificationForm) {

        //校验

        Qualification qualification =new Qualification();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(Qualification.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(qualificationForm, qualification, copyOptions);
        int insert = baseMapper.insert(qualification);
        if(insert>0){
            //利用json 实现类的相互转换
            com.wing.platform.order.model.request.QualificationForm qualificationForm1 = JSON.parseObject(JSON.toJSONString(qualification), com.wing.platform.order.model.request.QualificationForm.class);
            qualificationForm1.setEnterpriseId(qualification.getId());
            qualificationForm1.setCompanyId(new Long(BaseDataInit.ENTERPRISE_DATA.getId()));
            qualificationForm1.setUserId(JwtUtils.getLoginUserId());
            JsonResult jsonResult = platformClient.addQualification(qualificationForm1);
            if(jsonResult.getCode()!=200){
                return 0;
            }
        }
        return insert;
    }

    @Override
    public List<QualificationVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<QualificationVO> pageList4app(Page page, Map<String, Object> params) {
        List<QualificationVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public QualificationVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
